package com.kucowka.controllers;

import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kucowka.beans.JmxBean;
import com.kucowka.db.entities.User;
import com.kucowka.db.service.UserService;

@Controller(value = "/hotel")
public class UserControler {

	@Autowired
	private UserService userService;

	/*@Autowired
	@Qualifier(value = "jmxBeanProxy")
	private JmxBean jmxBean;*/

	@RequestMapping(value = "/time", method = RequestMethod.GET)
	public @ResponseBody
	String getTime(@RequestParam String name, @RequestParam String tm) {
		String result = "Time for " + name + " is " + new Date().toString()
				+ " " + tm;
		System.err.println(result);

		return result;
	}

	@RequestMapping("/index")
	public String mainPage(Map<String, Object> map) {

		return "index";
	}

	@RequestMapping("/user")
	public String listUsers(Map<String, Object> map) {

		map.put("user", new User());
		// System.err.println(userService.listAllUsers().iterator().next());
		map.put("usersList", userService.listAllUsers());
		//map.put("managedValue", jmxBean.getValue());
		map.put("managedValue", "ulala");

		return "user";
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addContact(@Valid @ModelAttribute("user") User user,
			BindingResult result) {

		if (result.hasErrors()) {
			return "user";
		}

		userService.createUser(user);

		return "redirect:/index.html";
	}

	@RequestMapping("/delete/{userId}")
	public String deleteContact(@PathVariable("userId") int userId) {

		userService.removeUser(userId);

		return "redirect:/index.html";
	}

}
